package com.wpw.dream.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadFactoryDemo {

	public static void main(String[] args) {
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName());
			}
		};

		MyThreadFactory my = new MyThreadFactory("pool");
		for (int i = 0; i < 10; i++) {
			Thread newThread = my.newThread(runnable);
			newThread.start();
		}
	}
}

class MyThreadFactory implements ThreadFactory {

	private String namePrefix;

	private final AtomicInteger threadNumber = new AtomicInteger(1);

	public MyThreadFactory(String prefix) {
		namePrefix = prefix;
	}

	@Override
	public Thread newThread(Runnable r) {
		return new Thread(r, namePrefix + "-" + "thread-" + threadNumber.getAndIncrement());
	}

}
